Data Types and Format Specifiers

Ints

An example of the int data type and the corresponding format specifiers.

#include <stdio.h>

int main(){
    /////// int
    //  - should be at least 16 bits (2 bytes)
    //  - Typically 4 bytes on most 32 and 64 bit machines
    //  - 4 bytes of 0s: 00000000 00000000 00000000 00000000

    printf("\nints:\n");
    int w;          // declaration
    w = 10;         // initialization
    int age = 21;   // both declaration and initilization

    // print the integer associated with x
    // %d is the format specifier for integers),
    printf("The value of w is %d\n", w);  

    // check the size on your machine
    printf("The size of an int is %lu bytes\n", sizeof(w));

    /////// short int
    //  - should be at least 16 bits (2 bytes)
    //  - Typically 2 bytes on most 32 and 64 bit machines
    printf("\nshort int:\n");
    short x = 20;
    printf("The value of x is `%d\n", x);
    printf("The size of a short int is %lu bytes\n", sizeof(x));


    /////// long int
    //  - should be at least 32 bits (4 bytes)
    //  - Typically 8 bytes on most 64 bit machines, but 4 bytes on 32 bit machines
    printf("\nlong int:\n");
    long y = 20;
    printf("The value of y is `%ld\n", y);
    printf("The size of a long is %lu bytes\n", sizeof(y));

    /////// long long int
    //  - should be at least 64 bits (8 bytes)
    //  - Typically 8 bytes
    printf("\nlong int:\n");
    long long int z = 20;
    printf("The value of z is `%lld\n", z);
    printf("The size of a long long is %lu bytes\n", sizeof(z));

    // unsigned versions of the above also
    //  - Range includes only positive values
    printf("\nunsigned int:\n");
    unsigned int u = 20;
    printf("The value of u is %u\n", u);
    printf("The size of an unsigned int is %lu bytes\n", sizeof(u));
}

Chars

An example of the int data type and the corresponding format specifiers.

#include <stdio.h>

int main(){
    // char 
    //      - single character
    //      - 1 byte of memory (8 bits)
    //      - can also store integers -128 to +127; 
    //      - note that integers correspond to ASCII values
    //      - unsigned char can store from 0 to 255 since they're all positive;

    char letter = 'a';
    printf("\nletter: %c\n", letter);
    printf("size: %lu byte\n", sizeof(letter));

    char another = 100;
    printf("\nanother - as char: %c\n", another);
    printf("another - as int: %d\n", another); 
    printf("size: %lu byte\n", sizeof(another));


    unsigned char anotherOne = 250; // outside bounds of ascii
    printf("\nanotherOne - as char: %c\n", anotherOne);
    printf("anotherOne - as int: %d\n", anotherOne);
    printf("size: %lu byte\n", sizeof(anotherOne));

    return 0;
}

Floats and Doubles

An example of floats and doubles.

#include <stdio.h>

int main(){

    /* float
        -  represented using a 32-bit IEEE 754 single precision floating point number
            -  8 bits for the exponent
            -  24 for the signicand
                - 1 bit for sign
                - 23 for mantissa
        - note that you cannot trust precion after a certain number of decimal places.
        - the following code showcases that. 
    */
    
    float x = 12.1234567890123456789012;
    printf("x: %.22f\n", x);
    long int numBits = sizeof(x) * 8;
    printf("size of x: %lu bytes\n", sizeof(x));
    printf("size of x: %lu bits \n", numBits);


    /* double
        -  represented using a 64-bit IEEE 754 double precision floating point number
            -  11 bits for the exponent
            -  53 bits for the significand
                - 1 bit for sign
                - 52 for mantissa

        - note that you cannot trust precion after a certain number of decimal places.
        - the following code showcases that.        
    */
    double y = 12.1234567890123456789012;
    printf("\ny: %.22lf\n", y);
    numBits = sizeof(y) * 8;
    printf("size of y: %lu bytes\n", sizeof(y));
    printf("size of y: %lu bits \n", numBits);

    return 0;
}